import os
import re





def chinese_to_arabic(cn: str) -> int | float:
    # 定义中文数字字符及其对应的阿拉伯数字
    chinese_numerals = {'零': 0, '一': 1, '二': 2, '三': 3, '四': 4,
                        '五': 5, '六': 6, '七': 7, '八': 8, '九': 9,
                        '〇': 0, '壹': 1, '贰': 2, '叁': 3, '肆': 4,
                        '伍': 5, '陆': 6, '柒': 7, '捌': 8, '玖': 9, '两': 2}
    # 定义中文数位单位及其对应的权重
    chinese_units = {'十': 10, '百': 100, '千': 1000, '万': 10000,
                     '亿': 100000000, '拾': 10, '佰': 100, '仟': 1000,
                     '萬': 10000, '億': 100000000}
    # 检查是否为小数
    if '点' in cn or '點' in cn:
        # 处理小数部分
        if '点' in cn:
            integer_part, decimal_part = cn.split('点', 1)
        else:
            integer_part, decimal_part = cn.split('點', 1)
        # 转换整数部分
        int_val = chinese_to_arabic(integer_part) if integer_part else 0
        # 转换小数部分
        dec_val = 0
        factor = 0.1
        for char in decimal_part:
            if char in chinese_numerals:
                dec_val += chinese_numerals[char] * factor
                factor *= 0.1
            else:
                raise ValueError(f"非法字符: {char}")
        return int_val + dec_val
    # 处理整数部分
    result = 0
    temp = 0
    last_unit = 1
    for char in cn:
        if char in chinese_numerals:
            temp = chinese_numerals[char]
        elif char in chinese_units:
            unit = chinese_units[char]
            if unit >= 10000:
                # 处理万亿级别的单位
                result = (result + temp) * unit
                temp = 0
                last_unit = unit
            else:
                # 处理十、百、千级别的单位
                if temp == 0:
                    temp = 1  # 处理"十"表示10的情况
                temp *= unit
                result += temp
                temp = 0
        else:
            raise ValueError(f"非法字符: {char}")
    # 处理剩余的临时值
    result += temp
    return result

# 示例使用
if __name__ == "__main__":

    # comp = re.compile(r'[一二三四五六七八九十百千万零]+')
    # change_dir = r"E:\txt\网游之无限\网游之无限\wav"
    # for file in os.listdir(change_dir):
    #     target = re.search(comp,file).group()
    #     new_name = file.replace(target,str(chinese_to_arabic(target)))
    #     old_path = os.path.join(change_dir,file)
    #     new_path = os.path.join(change_dir,new_name)
    #     os.rename(old_path,new_path)

    # test_cases = ["一", "十", "一百二十三", "一千二百三十四", "一万二千三百四十五",
    #               "十万零五千六百七十八", "一百二十三万四千五百六十七",
    #               "一千二百三十四万五千六百七十八", "一亿二千三百四十五万六千七百八十一",
    #               "零点五", "三点一四", "一百点零一", "十一点一一","一百零三"]
    # for case in test_cases:
    #     res= chinese_to_arabic(case)
    #     print(res)

    print("start")
    change_dir = r"E:\txt\网游之无限\wav"
    for file in os.listdir(change_dir):
        target,restring = re.search("第(.+)章",file).group(0,1)
        
        new_name = file.replace(target,restring)
        print(new_name)
        old_path = os.path.join(change_dir,file)
        new_path = os.path.join(change_dir,new_name)
        os.rename(old_path,new_path)